package com.hcbd.tdwt.ui.xingyu.activity;
import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.hcbd.tdwt.ui.xingyu.dialog.CustomDialog;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_preset_message)
public class PresetContentActivity extends BaseActivity {
    @ViewById
    LinearLayout customLL;
    @ViewById
    EditText newMessageET;
    @ViewById
    Button addContentBtn;
    private SharedPreferencesUtil sp;
    private String delimiter="~a-";
    @AfterViews
    protected void init() {
        sp = new SharedPreferencesUtil(this);
        initCustomContent();
    }
    @Click
    void headLeft(){
        finish();
    }
    @Click
    void addContentBtn(){
        String newMsg=getTextStr(newMessageET);
        if("".equals(newMsg)){
            Toast.makeText(PresetContentActivity.this,"内容不能为空！",Toast.LENGTH_SHORT).show();
            return;
        }
        String value=sp.getAttribute(Constant.CUSTOM_CONTENT);
        if(value==null||"".equals(value)){
            value=newMsg;
        }else{
            value=value+delimiter+newMsg;
        }
        sp.addAttribute(Constant.CUSTOM_CONTENT,value);
        newMessageET.setText("");
        initCustomContent();
        toast("添加成功");
    }
    private List<String>listStr;
    //初始化自定义内容
    public void initCustomContent(){
        String content=sp.getAttribute(Constant.CUSTOM_CONTENT);
        customLL.removeAllViews();
        if("".equals(content)||content==null){
            return;
        }
        final String []str=content.split(delimiter);
        listStr=new ArrayList(Arrays.asList(str));
        for(final String str1:listStr){
            View view= LayoutInflater.from(this).inflate(R.layout.custom_item, null, false);
            TextView textName=view.findViewById(R.id.textName);
            ImageView imageView=view.findViewById(R.id.del_custom);
            imageView.setOnClickListener(view1 -> showDelDialog(str1));
            textName.setText(str1);
            customLL.addView(view);
        }
    }
    public void showDelDialog(final String str1){
        new CustomDialog(this, "是否删除 '" + str1 + " '", CustomDialog.DEL_DIALOG, (dialog, content,view) -> {
            try {
                StringBuilder valueBuf = new StringBuilder();
                listStr.remove(str1);
                for (int i = 0; i < listStr.size(); i++) {
                    valueBuf.append(listStr.get(i));
                    if(!(i+1==listStr.size())){
                        valueBuf.append(delimiter);
                    }
                }
                sp.addAttribute(Constant.CUSTOM_CONTENT, valueBuf.toString());
                initCustomContent();
                dialog.dismiss();
            }catch (Exception e){
                e.printStackTrace();
            }
        }).show();
    }

}